'''
Created on 07.12.2012

@author: MBikman
'''
import sys
from osm_utils4 import CountryMap
import roadnet_agent as agent
import roadnet_problem as problem
import all_h
from datetime import *
import random

def heavyPart():
    '''
    Performs a highly resources consuming actions
    '''
    m = CountryMap()
    m.LoadMap2()
    return m

def lightPart(m):
    reload(agent)
    reload(problem)
    reload(all_h)
    reload(problem.problem_core)
    old = sys.stdout
    loadTime = str(datetime.now().minute)+"_"+ str(datetime.now().second)
    f = open("output\\output"+ loadTime +".txt" ,'w')
    f1 = open("output\\output"+ loadTime +".csv" ,'w')
    f1.write("Run Number,Source Key,Target Key,Heuristic,Airial Distance,Path Cost,Number of Nodes,Run Time\n")
#    sys.stdout = f
    count = 0
    p = getProblems()
    #random.seed(42)
    while count < 1:
        s = 257791#p[count][0]
        d = 723816#p[count][1]
        a = agent.RoadNetAgent()
        #ps = problem.RoadNetState(s,d, problem.ActionGenerator(m,1,0,0).WeightAction,m)
#        startTime = datetime.now()
#        res = a.solve(ps, all_h.ShortestH())
#        printPath(count, s, d, 'Shortest Distance', res,m,startTime,f1)
#        if not res:
#            continue
        ps = problem.RoadNetState(s,d, problem.ActionGenerator(m,0,1,0).WeightAction,m)
        startTime = datetime.now()
        res = a.solve(ps, all_h.FastestH())
        printPath(count, s, d, 'Time', res,m,startTime,f1)
#        m.car = "Mazda 3"
#        ps = problem.RoadNetState(s,d, problem.ActionGenerator(m,0,0,1).WeightAction,m)
#        startTime = datetime.now()
#        res = a.solve(ps, all_h.SavingH())
#        printPath(count, s, d, 'Petrol', res,m,startTime,f1)
        count = count + 1
        f.flush()
        f1.flush()
        old.write("Finished " + str(count)+"\n")
    sys.stdout = old
    f.close()
    print "finished - " + loadTime
    

def printPath(run_number, skey, tkey, heuristic, res,m,startTime,f1):
    if not res:
        print 'Run ' + str(run_number) + ' - Failed'
    else:
        print 'Run ' + str(run_number) + ' - Successed'
    print '\tHeuristic: ' + heuristic
    print '\tSource: (' + str(m.GetJunction(skey).lat) + ',' + str(m.GetJunction(skey).lon) + ')'
    print '\tDestination: (' + str(m.GetJunction(tkey).lat) + ',' + str(m.GetJunction(tkey).lon) + ')'
    if res:
        ad = m.JunctionDistance(skey,tkey)
        cost = sum([x.cost for x in res])
        nodes = len(res)
        run_time = (datetime.now() - startTime).total_seconds()
        print '\tAirial Distance: ' + str(ad) + "km"
        print '\tPath Cost: ' + str(cost) + "m"
        print '\tRes Length: ' + str(nodes) + " nodes"
        print '\tTime: ' + str(run_time) + "s"
        print ''
#        f1.write(str(run_number)+","+str(skey)+","+str(tkey)+","+heuristic+","+str(ad)+","+str(cost)+","+str(nodes)+","+str(run_time)+"\n")

if __name__ == '__main__':
    raise Exception('You should run wrapper.py instead!')
    
    
def getProblems():
    p = range(0,100)
    p[0]    = (522932,    464649)
    p[1]    = (539503,    829588)
    p[2]    = (27919,    145738)
    p[3]    = (392018,    691855)
    p[4]    = (920600,    39049)
    p[5]    = (433783,    542569)
    p[6]    = (645308,    719353)
    p[7]    = (273379,    379768)
    p[8]    = (712357,    94572)
    p[9]    = (489158,    583639)
    p[10]    = (100572,    287567)
    p[11]    = (171129,    816793)
    p[12]    = (355133,    76764)
    p[13]    = (134486,    479821)
    p[14]    = (624530,    833507)
    p[15]    = (607299,    95696)
    p[16]    = (899306,    283312)
    p[17]    = (432753,    443003)
    p[18]    = (64462,    476044)
    p[19]    = (855542,    205989)
    p[20]    = (406151,    773862)
    p[21]    = (230746,    792818)
    p[22]    = (402642,    407502)
    p[23]    = (491998,    399087)
    p[24]    = (623338,    646628)
    p[25]    = (440286,    66157)
    p[26]    = (748872,    807028)
    p[27]    = (777599,    393160)
    p[28]    = (897619,    802559)
    p[29]    = (758948,    472356)
    p[30]    = (392534,    704371)
    p[31]    = (88585,    906854)
    p[32]    = (4222,    479462)
    p[33]    = (371016,    729982)
    p[34]    = (363380,    130811)
    p[35]    = (247946,    442602)
    p[36]    = (535088,    161875)
    p[37]    = (316818,    510762)
    p[38]    = (48824,    123041)
    p[39]    = (375436,    861941)
    p[40]    = (862512,    739949)
    p[41]    = (752889,    697204)
    p[42]    = (203364,    728696)
    p[43]    = (410943,    678609)
    p[44]    = (743328,    164783)
    p[45]    = (28587,    363403)
    p[46]    = (814553,    19535)
    p[47]    = (2728,        177410)
    p[48]    = (590869,    393157)
    p[49]    = (213089,    127858)
    p[50]    = (692393,    566982)
    p[51]    = (437541,    730023)
    p[52]    = (646287,    633793)
    p[53]    = (424927,    575320)
    p[54]    = (887415,    86987)
    p[55]    = (394391,    530197)
    p[56]    = (135514,    646581)
    p[57]    = (85677,    571230)
    p[58]    = (660242,    80448)
    p[59]    = (863820,    822136)
    p[60]    = (474343,    579449)
    p[61]    = (724421,    785833)
    p[62]    = (207481,    692956)
    p[63]    = (366207,    94878)
    p[64]    = (786544,    836208)
    p[65]    = (689447,    314239)
    p[66]    = (417587,    368868)
    p[67]    = (592196,    701460)
    p[68]    = (692637,    497095)
    p[69]    = (586845,    672197)
    p[70]    = (486144,    592777)
    p[71]    = (727690,    645)
    p[72]    = (557822,    84823)
    p[73]    = (909452,    410774)
    p[74]    = (624925,    78332)
    p[75]    = (734359,    752224)
    p[76]    = (873575,    99833)
    p[77]    = (288174,    837256)
    p[78]    = (623797,    887560)
    p[79]    = (418085,    84885)
    p[80]    = (684427,    552580)
    p[81]    = (126648,    904073)
    p[82]    = (823543,    115786)
    p[83]    = (724949,    574484)
    p[84]    = (616778,    577665)
    p[85]    = (331035,    565413)
    p[86]    = (870900,    809609)
    p[87]    = (532362,    83922)
    p[88]    = (678872,    207334)
    p[89]    = (749996,    662218)
    p[90]    = (72583,    395287)
    p[91]    = (458410,    121294)
    p[92]    = (394394,    575723)
    p[93]    = (489926,    370237)
    p[94]    = (297288,    905175)
    p[95]    = (395192,    550956)
    p[96]    = (386114,    490477)
    p[97]    = (718110,    796697)
    p[98]    = (295432,    56786)
    p[99]    = (210844,    743626)
    return p
